package com.lengyue.imakiba.manage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.lengyue.imakiba.base.ImportExcel;
import com.lengyue.imakiba.data.Episode;
import com.lengyue.imakiba.logic.EpisodeLogic;

public class EpisodeManage extends Controller {
	public void getEpisode()
	{
		int animeId = 0;
		try {
			animeId = getParaToInt("animeId");
		} catch (Exception e) {
		}
		Page<Episode> episodes = EpisodeLogic.getEpisodeByAnimeSortByNumDescAndPagenate(getParaToInt("page"), getParaToInt("rows"), animeId);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", episodes.getTotalRow());
		jsonMap.put("rows", episodes.getList());
		renderJson(jsonMap);
	}
	public void saveEpisode()
	{
		new Episode().set("num", getPara("num")).set("name", getPara("name")).set("anime_id", getPara("anime_id"))
		.set("url1", getPara("url1")).set("url2", getPara("url2")).set("url3", getPara("url3"))
		.set("url4", getPara("url4")).set("url5", getPara("url5")).set("url6", getPara("url6"))
		.set("date", getPara("date")).save();
	}
	public void updateEpisode()
	{
		Episode.dao.findById(getPara("id")).set("num", getPara("num")).set("name", getPara("name")).set("anime_id", getPara("anime_id"))
		.set("url1", getPara("url1")).set("url2", getPara("url2")).set("url3", getPara("url3"))
		.set("url4", getPara("url4")).set("url5", getPara("url5")).set("url6", getPara("url6"))
		.set("date", getPara("date")).update();
	}
	public void deleteEpisode()
	{
		Episode.dao.deleteById(getPara("id"));
	}
	/**
	 * excel导入剧集
	 * @throws Exception
	 */
	@Before(Tx.class)
	@ActionKey("/login/importEpisode")//重写命名空间，防止session丢失。因为上传插件请求路径限制
	public void importEpisode() throws Exception
	{
		UploadFile uploadFile = getFile();
		int animeId = getParaToInt("animeId");
		ImportExcel ie = new ImportExcel();
		Record[] episodes = ie.importData(uploadFile.getFile());
		for(int i=0;i<episodes.length;i++)
		{
			int episodeId;
			//若没有设置剧集添加时间则默认使用当前时间
			if(episodes[i].get("date")==null)
			{
				episodes[i].set("date",new Date());
			}
			if((episodeId = isEpisodeExist(animeId, episodes[i].getInt("num")))==0)
			{
				Db.save("episode", episodes[i].set("anime_id", animeId));
			}else
			{
				Db.update("episode", episodes[i].set("anime_id", animeId).set("id", episodeId));
			}
		}
		uploadFile.getFile().delete();
	}
	/**
	 * 根据唯一索引animeId、num判断动漫剧集是否存在。
	 * @param animeId 动漫Id
	 * @param num 剧集号
	 * @return 存在则返回id否则返回0
	 */
	private int isEpisodeExist(int animeId,int num)
	{
		Episode episode = EpisodeLogic.getEpisodeByAnimeIdAndNum(animeId, num);
		if(episode==null)
		{
			return 0;
		}else
		{
			return episode.getInt("id");
		}
	}
}
